﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using EFcore;
using Domain.IRepositories;
using EFcore.Repositories;
using Application.UserApp;
//using MySQL.Data.EntityFrameworkCore.Extensions;
using Microsoft.EntityFrameworkCore;
using Application.ProductApp;
using Application.ArticleApp;
using Application.ReplyApp;
using Application.RoleApp;
using Application.MenuApp;
using Application.RoleMenuApp;
using Application.ConfigApp;
using Application.LogApp;
using Microsoft.AspNetCore.HttpOverrides;
using Application.ProductTypeApp;
using Application.ArticleTypeApp;
using Microsoft.AspNetCore.Http.Features;

namespace JFnetcore
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //上传文件大小设置
            services.Configure<FormOptions>(x =>
            {
                x.ValueLengthLimit = int.MaxValue;
                x.MultipartBodyLengthLimit = int.MaxValue; 
            });

            //添加数据上下文
            services.AddDbContext<JFDbContext>(options => options.UseMySql(Configuration.GetConnectionString("MySql"), b =>
            {
                b.MigrationsAssembly("JFnetcore");
            }));
            
            #region 依赖注入
            services.AddScoped<IUserRepository, UserRepository>();
            services.AddScoped<IUserAppService, UserAppService>();

            services.AddScoped<IProductRepository, ProductRepository>();
            services.AddScoped<IProductAppService, ProductAppService>();

            services.AddScoped<IProductTypeRepository, ProductTypeRepository>();
            services.AddScoped<IProductTypeAppService, ProductTypeAppService>();

            services.AddScoped<IArticleRepository, ArticleRepository>();
            services.AddScoped<IArticleAppService, ArticleAppService>();

            services.AddScoped<IArticleTypeRepository, ArticleTypeRepository>();
            services.AddScoped<IArticleTypeAppService, ArticleTypeAppService>();

            services.AddScoped<IReplyRepository, ReplyRepository>();
            services.AddScoped<IReplyAppService, ReplyAppService>();

            services.AddScoped<IRoleRepository, RoleRepository>();
            services.AddScoped<IRoleAppService, RoleAppService>();

            services.AddScoped<IMenuRepository, MenuRepository>();
            services.AddScoped<IMenuAppService, MenuAppService>();

            services.AddScoped<IRoleMenuRepository, RoleMenuRepository>();
            services.AddScoped<IRoleMenuAppService, RoleMenuAppService>();

            services.AddScoped<IConfigRepository, ConfigRepository>();
            services.AddScoped<IConfigAppService, ConfigAppService>();

            services.AddScoped<ILogRepository, LogRepository>();
            services.AddScoped<ILogAppService, LogAppService>(); 
            #endregion

            services.AddMvc();

            //Session服务
            services.AddSession(options => {
                //options.IdleTimeout = TimeSpan.FromHours(12);
                options.IdleTimeout = TimeSpan.FromMinutes(600);
            });

            services.AddDirectoryBrowser();//允许浏览目录的文件和文件夹
            
        }
        
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();


            //允许浏览目录的文件和文件夹
            app.UseDirectoryBrowser(new DirectoryBrowserOptions()
            {
                FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"wwwroot/upload")),
                RequestPath = new Microsoft.AspNetCore.Http.PathString("/wwwroot/upload")
            });
            app.UseDirectoryBrowser(new DirectoryBrowserOptions()
            {
                FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"wwwroot/KEupload")),
                RequestPath = new Microsoft.AspNetCore.Http.PathString("/wwwroot/KEupload")
            });

            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });

            //启用Session
            app.UseSession();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            //使用静态文件
            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Login}/{action=Index}/{id?}");
            });
        }
    }
}
